from aiogram import F
from aiogram_dialog import Dialog, Window
from aiogram_dialog.widgets.kbd import Button, ScrollingGroup, Select, Start, SwitchTo, WebApp
from aiogram_dialog.widgets.text import Format

from weed_shop.core.models.enums.user import UserRole
from weed_shop.tg_bot.dialogs.getters.delivery_zones import delivery_zones_getter
from weed_shop.tg_bot.dialogs.getters.users import user_info_getter, user_role_getter
from weed_shop.tg_bot.dialogs.getters.webapp import webapp_info_getter
from weed_shop.tg_bot.dialogs.widgets import LocaleText
from weed_shop.tg_bot.states import ClientSG, UserOrdersSG
from weed_shop.tg_bot.states.admin import AdminSG
from weed_shop.tg_bot.states.user import LanguageSG

from .handlers import not_confirm_age, select_delivery_zone, verify_user_age

start_window = Window(
    LocaleText("welcome", user="{event.from_user.username}"),
    WebApp(text=LocaleText("catalog-btn"), url=Format("{webapp_url}")),
    Start(
        LocaleText("orders-btn"),
        id="my_orders",
        state=UserOrdersSG.orders_history,
        when=F["user_orders_count"] > 0,
    ),
    SwitchTo(
        LocaleText("change-delivery-zone-btn"),
        id="change_delivery_zone_btn",
        state=ClientSG.select_delivery_zone,
    ),
    Start(LocaleText("change-lang-btn"), "change_lang_btn", LanguageSG.select_language),
    Start(
        LocaleText("admin-btn"),
        id="admin_panel",
        when=(F["user_role"].in_([UserRole.admin, UserRole.stuff])),
        state=AdminSG.admin,
    ),
    state=ClientSG.start,
    getter=(user_info_getter, user_role_getter, webapp_info_getter),
)

verify_age_window = Window(
    LocaleText("verify-age-msg"),
    Button(LocaleText("verify-btn"), id="verify_btn", on_click=verify_user_age),
    Button(LocaleText("not-verify-btn"), id="not_verify_btn", on_click=not_confirm_age),
    state=ClientSG.verify_age,
)

select_delivery_zone_window = Window(
    LocaleText("select-delivery-zone-msg"),
    LocaleText(
        "current-delizery-zone-msg",
        delivery_zone="{delivery_zone.title}",
        when=F["delivery_zone"],
    ),
    SwitchTo(
        LocaleText("keep-delivery-zone-btn"),
        id="keep_delivery_zone_btn",
        state=ClientSG.start,
        when=F["delivery_zone"],
    ),
    ScrollingGroup(
        Select(
            Format("{item.title}"),
            "delizery_zone_sel",
            lambda payment: payment.id,
            "delivery_zones",
            on_click=select_delivery_zone,
        ),
        width=2,
        height=4,
        id="delizeryzonesel",
        hide_on_single_page=True,
    ),
    SwitchTo(
        LocaleText("back-btn"),
        id="back_to_start",
        state=ClientSG.start,
        when=F["delivery_zone"],
    ),
    state=ClientSG.select_delivery_zone,
    getter=(delivery_zones_getter, user_info_getter),
)


start_dialog = Dialog(start_window, verify_age_window, select_delivery_zone_window)
